<?php

namespace Admin\Controller;

use Think\Controller;
use Admin\Model;

class LoginController extends Controller {

    // 注册接口
    public function register() {
        $param = $_GET;
        $accountname = $param['accountname'];
        $pwd = $param['pwd'];
        $shopname = $param['shopname'];
        $telphone = $param['telphone'];
        $address = $param['address'];
        $gps = $param['gps'];
        $x = $param['x'];
        $y = $param['y'];
        if (!isset($accountname) && empty($accountname)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '账号不能为空', 'data' => array()));
        }
        if (!isset($pwd) && empty($pwd)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '密码不能为空', 'data' => array()));
        }
        if (!isset($shopname) && empty($shopname)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '店铺名不能为空', 'data' => array()));
        }
        if (!isset($telphone) && empty($telphone)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '联系电话不能为空', 'data' => array()));
        }
        if (!isset($address) && empty($address)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '地址不能为空', 'data' => array()));
        }
        if (!isset($gps) && empty($gps) && !isset($x) && empty($x) && !isset($y) && empty($y)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '请在地图选择地址', 'data' => array()));
        }
        // 检查账号是否被注册
        $shopAccountModel = new \Admin\Model\ShopAccountModel();
        $result = $shopAccountModel->getFind(array('accountname' => $accountname));
        if (isset($result) && !empty($result)) {
            $this->ajaxReturn(array('code' => 10002, 'msg' => '账号已经被注册', 'data' => array()));
        }

        $data['accountname'] = $accountname;
        $data['pwd'] = strtoupper(md5($pwd));
        $data['shopname'] = $shopname;
        $data['telphone'] = $telphone;
        $data['inuse'] = 2;
        $data['address'] = $address;
        $data['gps'] = $gps;
        $data['x'] = $x;
        $data['y'] = $y;
        $data['createdt'] = date("Y-m-d H:i:s");
        $data['updatedt'] = date("Y-m-d H:i:s");
        $addRes = $shopAccountModel->addData($data);
        if (isset($addRes) && !empty($addRes)) {
            $this->ajaxReturn(array("code" => 10000, 'msg' => '注册成功！', 'data' => array()));
        } else {
            $this->ajaxReturn(array('code' => 10020, 'msg' => '服务器错误', 'data' => array()));
        }
    }

    // 登录
    public function index() {
        if (IS_POST) {
            $param = $_POST;
            $user = $param['username'];
            $pwd = strtoupper(md5($param['password']));
            $shopAccount = new \Admin\Model\ShopAccountModel();
            $result = $shopAccount->getFind(array('accountname' => $user));
            if (!isset($result) && empty($result)) {
                $this->error("账号错误");
//                $this->ajaxReturn(array('code' => 10001, 'msg' => '账号错误', 'data' => array()));
            }
            if ($result['pwd'] != $pwd) {
                $this->error("密码错误");
//                $this->ajaxReturn(array('code' => 10002, 'msg' => '密码错误', 'data' => array()));
            }
            if ($result['inuse'] != 1) {
                $this->error("账户被禁用 请联系管理员");
//                $this->ajaxReturn(array('code' => 10003, 'msg' => '账户被禁用 请联系管理员', 'data' => array()));
            }
            $_SESSION['account_user_id'] = $result['id'];
            $_SESSION['accountname'] = $result['accountname'];
            $_SESSION['shopname'] = $result['shopname'];
            $_SESSION['shop_type'] = $result['shop_type'];
            // 更新上架最后登录时间、ip
            $where['id'] = $result['id'];
            $data['lastlogindt'] = date("Y-m-d H:i:s");
            $data['lastloginip'] = $_SERVER['REMOTE_ADDR'];
            $shopAccount->saveData($where, $data);
            $this->success("登录成功", "/Admin/Index/index");
//            $this->ajaxReturn(array('code' => 10000, 'msg' => '登录成功', 'data' => array('accountid' => $result['id'])));
        } else {
            $this->display("/Login/login");
        }
    }

    public function test() {
        dump($_SESSION);die;
        echo json_encode(array('code' => 10001, 'msg' => '账号不能为空', 'data' => array()));
    }

}
